﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MvcEngine.Core.Repositories;
using MvcEngine.Core.Utils;
using MvcEngine.Core.Helpers;
using MvcEngine.Core.Base;
using MvcEngine.Core.Common;
using MvcEngine.Core.Pagination;

namespace MvcEngine.Core.Common
{
    public class CommonManager : BaseManager, ICommonManager
    {
        private readonly ILogRepository logRepository = null;

        public static ICommonManager Current
        {
            get { return IoC.Resolve<ICommonManager>(); }
        }

        public CommonManager(ILogRepository logRepository)
        {
            this.logRepository = logRepository;
        }

        public void Save(Log log)
        {
            log.CreatedDate = DateTime.Now;
            logRepository.Add(log);
        }

        public IPagination<Log> GetLogs(LoadOptions options)
        {
            //return CacheManager.Current.FromCache(string.Format(CacheKey.CommonKeys.LogPage, pageIndex.ToString(), count.ToString(), SiteContext.Current.Site.Id.ToString()), 
            //    CacheKey.CommonKeys.LogCategory, () => logRepository.GetAll(SiteContext.Current.Site.Id, pageIndex, count));
            return logRepository.GetAll(SiteContext.Current.Site.Id, options);
        }

        public IList<Log> GetLogs()
        {
            //return CacheManager.Current.FromCache(CacheUtils.MakeCacheKey(CacheKey.CommonKeys.LogAll, CacheKeys.Site),
            //    CacheKey.CommonKeys.LogCategory, () => logRepository.GetAll(SiteContext.Current.Site.Id));
            return logRepository.GetAll(SiteContext.Current.Site.Id);

        }

        public Log GetLog(Guid id)
        {
            //return CacheManager.Current.FromCache(string.Format(CacheKey.CommonKeys.LogId, id), CacheKey.CommonKeys.LogCategory,
            //() => logRepository.Get(id));
            return logRepository.Get(id);
        }

        public void DeleteLog(Guid id)
        {
            logRepository.Delete(id);
            //CacheManager.Current.RemoveFromCache(string.Format(CacheKey.CommonKeys.LogId, id));
        }

        public void DeleteLogs()
        {
            logRepository.DeleteAll(SiteContext.Current.Site.Id);
            //CacheManager.Current.RemoveFromCache(CacheUtils.MakeCacheKey(CacheKey.CommonKeys.LogAll, CacheKeys.Site), CacheKey.CommonKeys.LogCategory);
        }
    }
}
